//
//  StartViewController.m
//  GreenZone
//
//  Created by student on 15/6/15.
//  Copyright (c) 2015年 student. All rights reserved.
//

#define WinWidth [UIScreen mainScreen].bounds.size.width
#define WinHeight [UIScreen mainScreen].bounds.size.height

#define W(x) WinWidth*x/320.0  //屏幕适配，自动计算不同屏幕对应的x和y方向的坐标
#define H(y) WinHeight*y/568.0

#define SingleCircleViewWidth 100
#define CircleViewWidth WinWidth-20

#import "StartViewController.h"
#import "CircleView.h"
#import "DragImageView.h"
@interface StartViewController ()<CircleViewDelegate,DragImageViewDelegate>

@end

@implementation StartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createCircleViewMenuArr];
}

-(void)viewWillAppear:(BOOL)animated{
    [self showCircleView];
    //[self addAnimatonForView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  延迟实例化数组
 */
-(NSMutableArray *)viewArr{
    if (!_viewArr) {
        _viewArr = [[NSMutableArray alloc]init];
    }
    return _viewArr;
}

/**
 *  创建旋转菜单单个菜单的样式
 */
-(DragImageView *)createViewStyleWithTag:(int)tag{
    DragImageView *view = [[DragImageView alloc]initWithFrame:CGRectMake(0, 0, SingleCircleViewWidth, SingleCircleViewWidth)];
    view.center = CGPointMake(WinWidth/2, WinWidth/2);
    view.tag = tag;
    view.layer.cornerRadius = SingleCircleViewWidth/2;
    view.backgroundColor = [UIColor purpleColor];
    view.image = [UIImage imageNamed:@"desireIcon"];
    view.delegate = self;
    view.userInteractionEnabled = YES;
    return view;
}

/**
 *  创建旋转菜单的按钮组与视图
 */
-(void)createCircleViewMenuArr{
    for (int i=0; i<5; i++) {
        DragImageView *view = [self createViewStyleWithTag:i];
        [self.viewArr addObject:view];
    }
    self.circleMenu = [[CircleView alloc]initWithFrame:CGRectMake(0, 0, CircleViewWidth, CircleViewWidth)];
    self.circleMenu.center = CGPointMake(WinWidth/2, WinHeight/2);
    self.circleMenu.arrImages = self.viewArr;
    self.circleMenu.delegate = self;
    [self.view addSubview:self.circleMenu];
}

/**
 *  显示旋转菜单视图
 */
-(void)showCircleView{
    [self.circleMenu loadView];
}

/**
 *  添加旋转动画
 */
-(void)addAnimatonForView{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = @0;
    animation.toValue=@(2*M_PI);
    animation.duration = 10;
    animation.repeatCount = NSNotFound;
    [self.circleMenu.layer addAnimation:animation forKey:@"animation1"];
    animation.toValue=@(-2*M_PI);
    for (DragImageView *view in self.viewArr) {
        [view.layer addAnimation:animation forKey:@"animation2"];
    }
}

/**
 *  移除旋转动画
 */
-(void)removeAnimationForView{
    [self.circleMenu.layer removeAnimationForKey:@"animation1"];
    for (DragImageView *view in self.viewArr) {
        [view.layer removeAnimationForKey:@"animation2"];
    }
}

/**
 *  当检测到手指开始拖动菜单时的方法
 */
-(void)circleViewGestureStart{
    //[self removeAnimationForView];
}

/**
 *  当检测到手指停止拖动菜单时的方法
 */
-(void)circleViewGestureEnd{
    //[self addAnimatonForView];
}

/**
 *  页面跳转方法
 */
-(void)DragImageViewClick:(DragImageView *)view{
    int tag = view.tag;
    NSLog(@"点击了%d",tag);
}



- (IBAction)gotoBaike:(id)sender {
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Baike" bundle:nil];
    UITabBarController *tab = [board instantiateViewControllerWithIdentifier:@"tab"];
    //tab.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    CATransition *animation = [CATransition animation];
    animation.duration = 2;
    animation.type = @"rippleEffect";
    animation.subtype = kCATransitionFromRight;
    [self.view.window.layer addAnimation:animation forKey:nil];
    [self presentViewController:tab animated:YES completion:^{
        
    }];
}

- (IBAction)gotoGarden:(id)sender {
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Garden" bundle:nil];
    UIViewController *vc = [board instantiateViewControllerWithIdentifier:@"garden"];
    [self presentViewController:vc animated:YES completion:^{
        
    }];
}

- (IBAction)gotoMapTest:(id)sender {
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"MapTest" bundle:nil];
    UIViewController *vc = [board instantiateViewControllerWithIdentifier:@"test"];
    [self presentViewController:vc animated:YES completion:^{
        
    }];
}

@end
